<template>
  <div id="bills-to-review-v2">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item active ms-2" aria-current="page">
          账单 - 待审核
        </li>

        <li class="breadcrumb-item ms-auto">
          <router-link to="/bills">列表</router-link>
        </li>
        <li class="breadcrumb-item">
          <router-link to="/bills/toSettle">待结算</router-link>
        </li>
        <li class="breadcrumb-item">
          <router-link to="/ent/settlements">结算单</router-link>
        </li>
        <li class="breadcrumb-item me-2">
          <router-link to="/ent/receipts">收款单</router-link>
        </li>
      </ol>
    </nav>
    <div class="card bg-transparent">
      <div class="card-body py-1">
        <form class="row row-cols-lg-auto align-items-center">
          <div class="col-1">
            <select class="form-select" v-model.number="supplierId">
              <option value="0">所有供应商</option>
              <option
                :value="info.id"
                v-for="(info, index) in suppliers"
                :key="index"
              >
                {{ info.name }}
              </option>
            </select>
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="ticketStatus">
              <option value="0">正常票(含改签）</option>
              <option value="4">退票</option>
            </select>
          </div>
          <div class="col-1">
            <my-date-picker
              id="etdzDateBegin"
              v-model="etdzDate"
              placeholder="出票日期"
            ></my-date-picker>
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="reviewed">
              <option value="-1">审核状态</option>
              <option value="0">未审核</option>
              <option value="1">已审核</option>
            </select>
          </div>
          <div class="col-1">
            <select class="form-select" v-model.number="intlTicket">
              <option value="-1">国内国际</option>
              <option value="0">国内</option>
              <option value="1">港澳台、国际</option>
            </select>
          </div>
          <label>每页记录数</label>
          <div class="col-1">
            <select class="form-select" v-model.number="pageSize">
              <option value="100">100</option>
              <option value="200">200</option>
              <option value="500">500</option>
              <option value="5000">5000</option>
            </select>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-danger"
              @click.stop="autoReview()"
              :disabled="autoReviewing"
            >
              <template v-if="autoReviewing"> 自动预审中... </template>
              <template v-else> 自动预审 </template>
            </button>
          </div>
        </form>
      </div>
    </div>
    <div class="row">
      <div class="col-6">
        <review-bill-list
          :etdzDate="etdzDate"
          :ticket-status="ticketStatus"
          :pageSize="pageSize"
          :supplierId="supplierId"
          :reviewed="reviewed"
          :intlTicket="intlTicket"
        ></review-bill-list>
      </div>
      <div class="col-6">
        <template v-if="supplierBspType === 1">
          <review-bill-tpr
            :etdzDate="etdzDate"
            :ticket-status="ticketStatus"
            :intlTicket="intlTicket"
            :pageSize="pageSize"
          ></review-bill-tpr>
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import { searchSuppliers } from "@/api/basic-data";
import { autoReviewBillInfo, searchBills } from "@/api/bill";
import MyDatePicker from "@/components/my-datepicker.vue";
import MyModalPrompt from "@/components/modal/my-modal-prompt.vue";
import ReviewBillTpr from "./review-bill-tpr";
import ReviewBillList from "./review-bill-list";

export default {
  components: {
    MyDatePicker,
    ReviewBillTpr,
    ReviewBillList,
  },
  data() {
    return {
      pageSize: 200,
      etdzDate: "",
      reviewed: -1,
      intlTicket: -1,
      ticketStatus: 0,
      suppliers: [],
      supplierId: 0,
      supplierBspType: 0, //供应商的自动开票类型 0-无，1-BSP，2-GP

      autoReviewing: false,
    };
  },
  watch: {
    supplierId: function () {
      this.changeSupplierBspType();
    },
  },
  mounted: function () {
    this.searchSuppliers();
    // this.searchBills()
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", {
        loading: false,
      });
    },

    searchSuppliers: function () {
      if (this.suppliers.length > 0) return;
      const params = {
        "sc.pageNo": 1,
        "sc.pageSize": 500,
      };

      searchSuppliers(params, (v) => {
        this.suppliers = v.dataList;
      });
    },
    commonShowMessage: function (jsonResult) {
      if (jsonResult.status !== "OK") {
        this.showErrMsg("失败：" + jsonResult.errmsg);
      } else {
        this.searchBills();
        if (jsonResult.desc !== "") {
          this.showErrMsg(jsonResult.desc);
        } else {
          this.showErrMsg("操作成功");
        }
      }
    },
    autoReview: function () {
      // auto review
      this.autoReviewing = true;
      autoReviewBillInfo(
        (v) => {
          if (v.status === "OK") {
            this.showErrMsg(v.desc);
          } else {
            this.showErrMsg("出现错误");
          }
        },
        () => {
          this.autoReviewing = false;
        }
      );
    },
    changeSupplierBspType: function () {
      this.supplierBspType = 0;

      this.suppliers.forEach((info, index, arr) => {
        if (info.id === this.supplierId) {
          this.supplierBspType = info.isBsp;
          return;
        }
      });
    },
  },
};
</script>
